﻿#include "ObjectManager.h"

#include "MacroDefinition.h"

#ifndef CONFIGFILE_ON //如果没有开启配置文件，则使用本类默认设置
#define OBJECTINJECTION_ON //默认开启自动注入
#endif

ObjectManager* ObjectManager::objectManager=Q_NULLPTR;

ObjectManager::ObjectManager(void) : QObject()
{
	objectManager = this;
	objectManager->setObjectName("objectManager");
	objectCreation();
	objectInjection();
	objectInitialization();
	logSaver->saveLog("ObjectManager created");
}

ObjectManager::~ObjectManager(void)
{
  for(QString objName : objMap.keys())
    {
      removeFromObjMap(objName);
    }
}

ObjectManager* ObjectManager::getObjectManager()
{
  return Q_NULLPTR==objectManager?objectManager=new ObjectManager():objectManager;
  }

void ObjectManager::objectCreation()
{
  logSaver->saveLog("-------------------------objectCreation-------------------------");
  //所有用户自定义类都应继承QObject，且需要保留默认的无参构造函数。需要自动装配功能的用户自定义类还需要继承Object类并实现对应的接口。
  //一般创建单个对象请使用如下方式，其中A代表对象的类型。
  //addToObjMap<A>();
  //举例：addToObjMap<UdpIOer>();
  //若需要创建多个A类型的对象，则使用如下方式。objName需要用户自行指定，若已经存在指定的objName，则对象创建失败。
  //addToObjMap<A>(QString objName);
  //举例：addToObjMap<UdpIOer>("UdpIOer_1");addToObjMap<UdpIOer>("UdpIOer_2");
  //若用户需要自行完成对象的创建过程，则使用如下方式。需要用户输入新创建的A类对象的指针a，若用户没有指定objName，则会根据类名称首字母小写规则自动生成一个objName。
  //addToObjMap<A>(A* a,QString objName);
  //举例：addToObjMap<UdpIOer>((UdpIOer*)UdpIOer_React::getUdpIOer_React());
  logSaver->saveLog("-------------------------objectCreation-------------------------");
}

#ifdef OBJECTINJECTION_ON
void ObjectManager::objectInjection()
{
	logSaver->saveLog("-------------------------objectInjection-------------------------");
	for (QObject* obj : objMap.values())
	{
		ObjAutoInject* temp = dynamic_cast<ObjAutoInject*>(obj);
		if (temp)
		{
			temp->objectAutoInjection(&objMap);
		}
	}
	logSaver->saveLog("-------------------------objectInjection-------------------------");
}
#else
void ObjectManager::objectInjection()
{
  //若没有开始对象自动注入，则需要用户在本函数内手动完成对象注入工作。
}
#endif

void ObjectManager::objectInitialization()
{
  logSaver->saveLog("-------------------------objectInitialization-------------------------");
  //对象的初始化工作可以在本函数内进行，目前还未实现自动初始化
  //对象指针可以通过如下方式获取
  //getObjFromMap<TcpListener>()
  //举例：
  //(getObjFromMap<TcpListener>())->listen(QHostAddress::Any, LISTENPORT);
  //(getObjFromMap<UdpIOer>())->bind(LISTENPORT);
  logSaver->saveLog("-------------------------objectInitialization-------------------------");
}

void ObjectManager::removeFromObjMap(QObject* obj)
{
	if (Q_NULLPTR == obj)
	{
		return;
	}
	removeFromObjMap(obj->objectName());
}

void ObjectManager::removeFromObjMap(QString objName)
{
	QString name = objName;
	if (name.isEmpty())
	{
		logSaver->saveLog(QString("removeFromObjMap , obj name is empty"));
		return;
	}
	if (!objMap.contains(name))
	{
		logSaver->saveLog(QString("removeFromObjMap , obj %1 is not in objMap").arg(name));
		return;
	}
	objMap.take(name)->deleteLater();
	logSaver->saveLog(QString("removeFromObjMap , obj %1 is removed from objMap").arg(name));
}

void ObjectManager::addToObjMap(QObject* obj, QString objName)
{
	if (Q_NULLPTR == obj)
	{
		logSaver->saveLog("addToObjMap , obj is null");
		return;
	}
	obj->setObjectName(objName);
	QString name = obj->objectName();
	if (name.isEmpty())
	{
		logSaver->saveLog(QString("addToObjMap , obj name is empty"));
		obj->deleteLater();
		return;
	}
	if (objMap.contains(name))
	{
		logSaver->saveLog(QString("addToObjMap , obj %1 is already in objMap").arg(name));
		obj->deleteLater();
		return;
	}
	objMap.insert(name, obj);
	logSaver->saveLog(QString("addToObjMap , obj %1 is added to objMap").arg(name));
}







